package com.legal.app.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.legal.common.annotation.Log;
import com.legal.common.core.controller.BaseController;
import com.legal.common.core.domain.AjaxResult;
import com.legal.common.enums.BusinessType;
import com.legal.app.domain.Message;
import com.legal.app.service.IMessageService;
import com.legal.common.utils.poi.ExcelUtil;
import com.legal.common.core.page.TableDataInfo;
import com.legal.app.domain.vo.MessageSendVO;
import com.legal.app.domain.vo.MessageReadVO;
import com.legal.common.utils.SecurityUtils;
import com.legal.app.domain.vo.MessageListItemVO;

/**
 * 消息Controller
 * 
 * @author ALe
 * @date 2025-05-30
 */
@RestController
@Api(tags = "消息管理")
@RequestMapping("/app/message")
public class MessageController extends BaseController {
    @Autowired
    private IMessageService messageService;

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('app:message:list')")
    @GetMapping("/list")
    public AjaxResult list() {
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(messageService.getMessageSessionList(userId));
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('app:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(messageService.selectMessageById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('app:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message) {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('app:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message) {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('app:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }

    /**
     * 发送消息
     */
    @PostMapping("/send")
    public AjaxResult send(@RequestBody MessageSendVO vo) {
        Long userId = SecurityUtils.getUserId();
        int result = messageService.sendMessage(vo, userId);
        return result > 0 ? AjaxResult.success() : AjaxResult.error("发送失败");
    }

    /**
     * 标记消息为已读
     */
    @PostMapping("/read")
    public AjaxResult read(@RequestBody MessageReadVO vo) {
        Long userId = SecurityUtils.getUserId();
        int result = messageService.readMessage(vo.getMessageId(), userId);
        return result > 0 ? AjaxResult.success() : AjaxResult.error("操作失败");
    }
}
